package com.ttmusic.view.adapters;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ttmusic.activitys.MainActivity;
import com.ttmusic.activitys.R;
import com.ttmusic.dao.MusicInfoDao;
import com.ttmusic.entity.AbstractMusic;
import com.ttmusic.entity.AbstractMusic.MusicType;
import com.ttmusic.utils.MusicUtils;
import com.ttmusic.utils.baidu.BaiduMusicUtil;
import com.ttmusic.utils.baidu.Song;
import com.ttmusic.utils.baidu.SongInfoDownload;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class SearchSongAdapter extends MusicAdapter {
	
	private Context mContext;
	//搜索到的结果
	private List<AbstractMusic> searchSongs = new ArrayList<AbstractMusic>();
	
	public SearchSongAdapter(Context mContext) {
		super(mContext, null, false);
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		return searchSongs.size();
	}

	@Override
	public AbstractMusic getItem(int position) {
		return searchSongs.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	private Handler downloadHandler = new Handler() {
		//MusicUtils mu = new MusicUtils(mContext);
		@Override
		public void handleMessage(Message msg) {
			if(msg.what == 200) {
				SearchSongAdapter.this.notifyDataSetChanged();
				List<AbstractMusic> musicInfoList = MusicInfoDao.getInstance().findAll();
				MainActivity.localMusicFragment.changeData(musicInfoList);
			}else {
				Log.e("SearchSongAdapter", msg.obj.toString());
			}
		}
	};
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder mViewHolder = null;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_search_song, null);
            mViewHolder.song_text = (TextView) convertView.findViewById(R.id.song_title);
            mViewHolder.letter_text = (TextView) convertView.findViewById(R.id.catalog);
            mViewHolder.artist_tv = (TextView) convertView.findViewById(R.id.artist_tv);
            mViewHolder.duration_tv = (TextView) convertView.findViewById(R.id.duration_tv);
            mViewHolder.icon_download = (ImageView)convertView.findViewById(R.id.icon_download);
            mViewHolder.song_item = (LinearLayout) convertView.findViewById(R.id.song_item);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        final SongInfoDownload song = (SongInfoDownload)this.getItem(position);
        File f = new File(BaiduMusicUtil.DOWNLOAD_SONG_SAVEPATH + song.getArtist() + " - " + song.getTitle() + "." + song.getFormat());
        if(f.exists()) {
        	//文件存在就显示已下载
        	mViewHolder.icon_download.setImageResource(R.drawable.icon_downloaded);
			mViewHolder.icon_download.setFocusable(false);
			mViewHolder.icon_download.setClickable(false);
			song.setType(MusicType.Local);
        }else {
        	//文件不存在就显示下载按钮并添加点击事件
        	mViewHolder.icon_download.setImageResource(R.drawable.icon_download1);
			mViewHolder.icon_download.setFocusable(true);
			mViewHolder.icon_download.setClickable(true);
			mViewHolder.icon_download.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					//开始下载
					Toast.makeText(mContext, "已添加到下载任务", Toast.LENGTH_LONG).show();
					BaiduMusicUtil.download(song, downloadHandler);
				}
			});
        }
        mViewHolder.song_text.setText(song.getSongName());
        mViewHolder.artist_tv.setText(song.getArtistName() + " - " + song.getAlbumName());
        mViewHolder.duration_tv.setText(song.getDurationStr());
        mViewHolder.letter_text.setVisibility(View.GONE);
        return convertView;
	}

	public List<AbstractMusic> getSearchSongs() {
		return searchSongs;
	}

	public void setSearchSongs(List<AbstractMusic> searchSongs) {
		this.searchSongs = searchSongs;
	}

	final static class ViewHolder {
        TextView song_text;
        TextView letter_text;
        TextView artist_tv;
        TextView duration_tv;
        ImageView icon_download;
        LinearLayout song_item;
    }
}
